嗨嗨!我是Winnie溫尼~
let rainbowColorNumber = 7
let rainbowColor = ["紅","橙","黃","綠","藍","靛","紫" ]
咦~第一行是之前介紹過的變數宣告,宣告彩虹的顏色數量等於7,那第二行這個有大括號的是什麼呢?就是今天要介紹的「陣列(Array)」哩!而為什麼會有陣列呢?陣列是用來將相同性質的資料集合起來。
陣列的值要使用[]包覆!let 變數名稱 = [值,值,...]
例:
let number = [1,6,8]
let rainbowColor = ["紅","橙","黃","綠","藍","靛","紫" ]
陣列的排序是從0開始!變數名稱[索引值]
例:取出彩虹顏色中的綠色
let rainbowColor = ["紅","橙","黃","綠","藍","靛","紫" ]
// 0, 1, 2, 3, 4, 5, 6
console.log(rainbowColor[3]) //綠
let 新變數名稱 = 原變數名稱[索引值]
例:我喜歡的是彩虹顏色中的黃色
let rainbowColor = ["紅","橙","黃","綠","藍","靛","紫" ]
// 0, 1, 2, 3, 4, 5, 6
let colorILike = rainbowColor[2];
console.log(colorILike) //黃
變數名稱.length
例:我想要知道rainbowColor的陣列中有幾種顏色
let rainbowColor = ["紅","橙","黃","綠","藍","靛","紫" ]
let colorNum = rainbowColor.length;
console.log(colorNum) //7
新手上路,如文章有錯誤或需修正之處,懇請大家多多指教!
那麼,我們明天見囉~